package web;

import bean.Admin;
import bean.BlackList;
import bean.Items;
import bean.Users;
import service.AdminService;
import service.Impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

/**
 * @author 栋
 * @date 2022/12/5
 * @description
 */
@WebServlet("/adminServlet")
public class AdminServlet extends baseServlet{
    AdminService adminService=new AdminServiceImpl();
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String account = req.getParameter("account");
        String pwd=req.getParameter("pwd");
        Admin admin = adminService.login(account,pwd);
        if(admin==null){
            req.setAttribute("msg", "账号或密码错误！");
            req.setAttribute("account", account);
            req.getRequestDispatcher("/pages/admin/login.jsp").forward(req, resp);
        }else{
            req.getSession().setAttribute("admin",admin);
            req.getRequestDispatcher("/pages/admin/main.jsp").forward(req, resp);

        }
    }

    protected void showShenHeItems(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Items> items = adminService.showShenHeItems();
        if(items.size()==0){
            req.setAttribute("msg", "空！");
            req.getRequestDispatcher("/pages/admin/shenHe.jsp").forward(req, resp);
        }else{
            req.getSession().setAttribute("shenHeItems", items);
            req.getRequestDispatcher("/pages/admin/shenHe.jsp").forward(req, resp);
        }
    }
    protected void shenHeItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String option = req.getParameter("option");
        String item_id = req.getParameter("item_id");
        Admin admin = (Admin)req.getSession().getAttribute("admin");
        if(option.equals("1")){
            adminService.updateItemsInfor(item_id,admin.getAdmin_id(),"是");
        }else{
            adminService.updateItemsInfor(item_id,admin.getAdmin_id(),"否");
        }
        List<Items> items = adminService.showShenHeItems();
        if(items.size()==0){
            req.setAttribute("msg", "空！");
            req.getSession().setAttribute("shenHeItems", null);
            req.getRequestDispatcher("/pages/admin/shenHe.jsp").forward(req, resp);
        }else{
            req.getSession().setAttribute("shenHeItems", items);
            req.getRequestDispatcher("/pages/admin/shenHe.jsp").forward(req, resp);
        }
    }
    protected void manageUserInfor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Users> users = adminService.getallUsers();
        req.getSession().setAttribute("allUsers",users);
        req.getRequestDispatcher("/pages/admin/UserInfor.jsp").forward(req, resp);

    }
    protected void addToblackList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user_id = req.getParameter("user_id");
        String introduce = req.getParameter("introduce");
        Admin admin = (Admin)req.getSession().getAttribute("admin");
        BlackList b=new BlackList(user_id,introduce, Date.valueOf(LocalDate.now()), Time.valueOf(LocalTime.now()),admin.getAdmin_id());
        if(adminService.addToBlackList(b)){
            req.setAttribute("msg", "拉黑成功");
            req.getRequestDispatcher("/pages/admin/UserInfor.jsp").forward(req, resp);
        }else{
            req.setAttribute("msg", "该用户已经在黑名单中了");
            req.getRequestDispatcher("/pages/admin/UserInfor.jsp").forward(req, resp);
        }



    }
    protected void jiechulahei(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user_id = req.getParameter("user_id");
        adminService.jiechulahei(user_id);
        List<BlackList> allBlack = adminService.getAllBlack();
        req.setAttribute("msg", "解除成功");
        if(allBlack.size()==0){
            req.getSession().setAttribute("blackList",null);
            req.getRequestDispatcher("/pages/admin/showBlackList.jsp").forward(req, resp);
        }else{
            req.getSession().setAttribute("blackList",allBlack);
            req.getRequestDispatcher("/pages/admin/showBlackList.jsp").forward(req, resp);
        }
    }
    protected void showBlackList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<BlackList> allBlack = adminService.getAllBlack();
        if(allBlack.size()==0){
            req.setAttribute("msg", "空");
            req.getSession().setAttribute("blackList",null);
            req.getRequestDispatcher("/pages/admin/showBlackList.jsp").forward(req, resp);
        }else{
            req.getSession().setAttribute("blackList",allBlack);
            req.getRequestDispatcher("/pages/admin/showBlackList.jsp").forward(req, resp);
        }
    }

}
